﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NanoERP.Common;
using System.Windows.Forms;
using NanoERP.Model;
using NanoERP.BaseInfo;
using WeifenLuo.WinFormsUI.Docking;

namespace NanoERP
{
    /// <summary>
    /// 多文档窗体激活创建
    /// </summary>
    [Serializable]
    public class WinFormsUI
    {
        /// <summary>
        /// 激活或创建子窗体
        /// </summary>
        /// <param name="dockForm">要激活(或者创建)的窗体</param>
        /// <param name="dockPanel">DockPanel容器</param>
        public static void ActivateOrCreateChildForm(DockContent dockForm, DockPanel dockPanel)
        {
            IDockContent frm = FindDocument(dockForm.Text, dockPanel);
            if (frm == null)
                dockForm.Show(dockPanel);
            else
                frm.DockHandler.Activate();
        }

        /// <summary>
        /// 激活或创建子窗体
        /// </summary>
        /// <param name="dockForm">要激活(或者创建)的窗体</param>
        /// <param name="dockPanel">DockPanel容器</param>
        /// <param name="dockState">窗体停靠状态</param>
        public static void ActivateOrCreateChildForm(DockContent dockForm, DockPanel dockPanel, DockState dockState)
        {
            IDockContent frm = FindDocument(dockForm.Text, dockPanel);
            if (frm == null)
                dockForm.Show(dockPanel, dockState);
            else
                frm.DockHandler.Activate();
        }

        /// <summary>
        /// 查找多标签窗口
        /// </summary>
        /// <param name="text"></param>
        /// <param name="dockPanel"></param>
        /// <returns></returns>
        static IDockContent FindDocument(string text, DockPanel dockPanel)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
                return Form.ActiveForm.MdiChildren.FirstOrDefault(f => f.Text.Equals(text)) as IDockContent;
            else
                return dockPanel.Contents.FirstOrDefault(d => d.DockHandler.TabText.Equals(text));
        }
    }
}
